import request from "superagent";
import {
    serverPath,
    json
} from "../../common/config.js";
import {
    prepareRequest,
    serviceInterceptor
} from "../../common/utils";
import validate from 'validate.js'
import {
    pageValidator
} from "../validator/pageValidator.js";

class GradesService{
    constructor(){

    }
    @serviceInterceptor()
    async getGrades(pageIndex, pageSize, searchCondition) {
        if (!pageIndex)
            pageIndex = 1;
        if (!pageSize)
            pageSize = 0;

        var data = { pageIndex: pageIndex, pageSize: pageSize };
        var result = await validate(data, pageValidator);
        if (!result) {
            let req = request.get(`${serverPath}grades`)
                .query(data)
                .query(searchCondition)
                .accept(json);

            return await prepareRequest(req);
        } else {
            return result;
        }
    }
    @serviceInterceptor()
    async getGradesById(gradesId) {
        if (!gradesId)
            return;

        let req = request.get(`${serverPath}grades/${gradesId}`)
            .accept(json);
        return await prepareRequest(req);
    }
    @serviceInterceptor()
    async createGrades(data) {
        if (!data)
            return;
        let req = request.post(`${serverPath}grades`)
            .send(data)
            .accept(json);

        return await prepareRequest(req);

    }
    @serviceInterceptor()
    async updateGrades(gradesId, data) {
        if (!data)
            return;
        if (!gradesId)
            return;
        let req = request.put(`${serverPath}grades/${gradesId}`)
            .send(data)
            .accept(json);

        return await prepareRequest(req);

    }

    @serviceInterceptor()
    async deleteGrades(gradesId){
        if (!gradesId)
        return;
    let req = request.delete(`${serverPath}grades/${gradesId}`)
        .accept(json);

       return await prepareRequest(req);
    }
}

export default GradesService;